<?php

namespace app\index\controller;

use app\common\tool\RedisClient;
use \think\Controller;
use app\blog\model\ArticleModel;


class Article extends BaseController
{
    public function read($id)
    {
        $redis = RedisClient::get_redis();
        // 从redis中拿到数据
        $article = $redis->get('article_' . $id);
        // 如果redis中数据过期了，就去数据库拿
        if (empty($article)) {
            $article = ArticleModel::read($id);
        } else {
            $article = json_decode($article, true);
        }

        $article['click_times'] = $article['click_times'] + 1;

        $data = $article;

        $data['content'] = html_entity_decode($data['content']);
        $this->assign('article', $data);
        // 再次存回redis中
        $article = json_encode($article);
        $redis->set('article_' . $id, $article);


        //数据库中点击次数加一
        ArticleModel::click_once($id);
        return view();
    }
}